package com.qianxx.viewlibrary.effects.scratcharea;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.qianxx.viewlibrary.R;

/**
 * 刮奖控件
 */
public class ScratchArea extends View {
    //控件宽度
    int width;

    //控件高度
    int height;

    //手势滑动的路径
    private Path mPath;

    //绘制滑动手势的画笔
    private Paint mOutterPaint;

    //记录用户path每次的开始坐标值
    private int mLastX;
    private int mLastY;

    //手势滑动的宽度
    private float strokeWidth = 60;

    //遮罩层的画布
    private Canvas mCanvas;

    //遮罩层
    private Bitmap bitmap;

    //遮罩图片id
    private int mask;

    //遮罩层上的图片
    private Bitmap image;

    //刮奖的文字信息
    private String text = "谢谢惠顾";

    //绘制文字的画笔
    private Paint textPaint;

    //字体大小
    private float textSize = 30;

    //字体颜色
    private int textColor = Color.BLACK;


    public ScratchArea(Context context) {
        this(context, null);
    }

    public ScratchArea(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ScratchArea(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.ScratchArea);
        strokeWidth = ta.getDimension(R.styleable.ScratchArea_ScratchArea_strokeWidth, 60);
        textSize = ta.getDimension(R.styleable.ScratchArea_ScratchArea_textSize, 30);
        textColor = ta.getColor(R.styleable.ScratchArea_ScratchArea_textColor, Color.BLACK);
        mask = ta.getResourceId(R.styleable.ScratchArea_ScratchArea_mask, R.drawable.baidi);
        ta.recycle();

        textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(textSize);
        textPaint.setColor(textColor);
        textPaint.setStyle(Paint.Style.FILL);

        mOutterPaint = new Paint();
        mOutterPaint.setColor(Color.RED);
        mOutterPaint.setAntiAlias(true);
        mOutterPaint.setDither(true);
        mOutterPaint.setStrokeJoin(Paint.Join.ROUND);//设置圆角
        mOutterPaint.setStrokeCap(Paint.Cap.ROUND);
        mOutterPaint.setStyle(Paint.Style.FILL);
        mOutterPaint.setStrokeWidth(strokeWidth);//设置画笔宽度
        mPath = new Path();

        //遮罩图片
        image = BitmapFactory.decodeResource(getResources(), mask);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        //获取控件宽度
        width = MeasureSpec.getSize(widthMeasureSpec);

        //获取控件高度
        height = MeasureSpec.getSize(heightMeasureSpec);

        //设置控件宽高
        setMeasuredDimension(width, height);

        //构建遮罩层
        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        //构建新画布，并将即将绘制的内容绘制到遮罩层上
        mCanvas = new Canvas(bitmap);

        //在遮罩层上绘制图片
        mCanvas.drawBitmap(image, null, new Rect(0, 0, width, height), null);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //在底层画布上绘制文字
        canvas.drawText(text, (width - getTextWidth()) / 2, (height + getTextHeight()) / 2, textPaint);

        //在遮罩层上绘制手势路径
        mOutterPaint.setStyle(Paint.Style.STROKE);
        mOutterPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));//Mode.DST_OUT改模式就类似橡皮檫，这个属性设置是关键
        mCanvas.drawPath(mPath, mOutterPaint);

        //将整个遮罩层绘制到底层画布上
        canvas.drawBitmap(bitmap, null, new Rect(0, 0, width, height), null);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        int x = (int) event.getX();
        int y = (int) event.getY();

        switch (action) {
            case MotionEvent.ACTION_DOWN://按下
                //记录按下的时候的X和Y值，以便于之后移动的时候绘制
                mLastX = x;
                mLastY = y;
                mPath.moveTo(mLastX, mLastY);
                break;
            case MotionEvent.ACTION_MOVE://移动
                //拿到用户移动的X绝对值，Y轴绝对值
                int dx = Math.abs(x - mLastX);
                int dy = Math.abs(y - mLastY);
                //用户滑动超过3像素才会改变，这个可以不做，做只是为了避免很频繁的相应而已。
                if (dx > 3 || dy > 3) {
                    mPath.lineTo(x, y);
                }
                mLastX = x;
                mLastY = y;
                break;
        }
        invalidate();//刷新UI
        return true;
    }

    /**
     * 获取文字宽度
     *
     * @return
     */
    private int getTextWidth() {
        Rect rect = new Rect();
        textPaint.getTextBounds(text, 0, text.length(), rect);
        return rect.width();
    }

    /**
     * 获取文字高度
     *
     * @return
     */
    private int getTextHeight() {
        Rect rect = new Rect();
        textPaint.getTextBounds(text, 0, text.length(), rect);
        return rect.height();
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
        invalidate();
    }

    public int getMask() {
        return mask;
    }

    public void setMask(int mask) {
        this.mask = mask;
        invalidate();
    }

    public float getStrokeWidth() {
        return strokeWidth;
    }

    public void setStrokeWidth(float strokeWidth) {
        this.strokeWidth = strokeWidth;
        invalidate();
    }

    public float getTextSize() {
        return textSize;
    }

    public void setTextSize(float textSize) {
        this.textSize = textSize;
        invalidate();
    }

    public int getTextColor() {
        return textColor;
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
        invalidate();
    }
}